package com.ysq.excavator.util;

import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.StringTokenizer;

import org.ansj.util.FilterModifWord;

/**
 * 分词ansj 工具类
 * 
 * @author Administrator
 * 
 */
public class AnsjUtil {
	public static String rootPath;
	
	/**
	 * 标准的停用词库
	 */
	public final static String NORMAL_STOP_WORDS = "library/stopwords.dic";
	
	/**
	 * 用户自定义的停用词库
	 */
	public final static String USER_STOP_WORDS = "library/userLibrary/userStopWords.dic";
	
	private static String userLibrary = "/library/userLibrary/userLibrary.dic";

	private static String stopLibrary = "/library/stop/stopLibrary.dic";

	private static String ambiguityLibrary = "/library/ambiguity.dic";

	private static String configPath = "/WEB-INF/classes/library.properties";// properties

	/**
	 * 更新 ansj 的词库路径的properties文件
	 */
	public static void updateAnsjSetting(){
		configPath = rootPath + configPath;
		
		Properties prop = new Properties();

		try {
			prop.load(new FileInputStream(configPath));
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		userLibrary = rootPath + userLibrary;
		stopLibrary = rootPath + stopLibrary;
		ambiguityLibrary = rootPath + ambiguityLibrary;

		updateProperties(prop, "userLibrary", userLibrary);
		updateProperties(prop, "stopLibrary", stopLibrary);
		updateProperties(prop, "ambiguityLibrary", ambiguityLibrary);
	}

	/**
	 * 更新（或插入）一对properties信息(主键及其键值)
	 * 如果该主键已经存在，更新该主键的值
	 * 如果该主键不存在，则插件一对键值。
	 * @param keyname 键名
	 * @param keyvalue 键值
	 */
	public static void updateProperties(Properties prop,String keyName, String keyValue) {
		try {
			// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果
			OutputStream fos = new FileOutputStream(configPath);
			prop.setProperty(keyName, keyValue);
			// 以适合使用 load 方法加载到 Properties 表中的格式
			prop.store(fos, "Update '" + keyName + "' value");
			LoggerUtil.logInfo("upde properties " + keyName + " : " + keyValue);
			
		} catch (IOException e) {
			LoggerUtil.logError("updateProperties error...", e);
		}
	}
	
	public static void main(String[] args) {
		updateAnsjSetting();
	}
	
	/**
	 * 获得停用词 map
	 * @return
	 * @throws Exception
	 */
	public static HashMap<String,String> getStopWordsMap(String stopWordsType,double quota){
		HashMap<String, String> updateDic = new HashMap<String, String>();
		
		InputStreamReader input = null;
		BufferedReader reader = null;
		try {
			input = new InputStreamReader(new FileInputStream(rootPath + stopWordsType), "UTF-8");
			
			reader = new BufferedReader(input);
			
			String line = null;
			
			int i = 0;
			
			while((line = reader.readLine()) != null){
				i++;
				try {
					StringTokenizer tokenizer = new StringTokenizer(line);
					
					String word = tokenizer.nextToken("\t");
					double quotaToken = Double.parseDouble(tokenizer.nextToken("\t"));
					
					if(quotaToken <= quota){
						updateDic.put(word, FilterModifWord._stop);
					}
				} catch (Exception e) {
					LoggerUtil.logError("error when process stopwords library in line " + i,e);
				}
			}
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(input != null){
				try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return updateDic;
	}
	
	
	/**
	 * 初始化停用词库
	 * 默认使用标准词库
	 * @param stopWordsType 停用词库
	 * @param quota 停用的指标，通常为0.1就可以了，有效值0-1，值越大，停用的范围越大
	 */
	public static void loadStopWords(String stopWordsType,double quota){
		
		try {
			HashMap<String, String> updateDic = null;
			if(USER_STOP_WORDS.equalsIgnoreCase(stopWordsType)){
				updateDic = getStopWordsMap(USER_STOP_WORDS,quota);
			}else{
				updateDic = getStopWordsMap(NORMAL_STOP_WORDS,quota);
			}
			
			LoggerUtil.logInfo("stopWordsMap size is " + updateDic.size());
			
			FilterModifWord.setUpdateDic(updateDic);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
